new Vue({
    el: '#mainBody',
    data: function () {
        return {
            dialogVisible: false, //数据上传dialog
            templatDialogVisible: false, //数据类型下载dialog
            addDataTypeVisible: false, //创建数据类型dialog
            searchForm: {
                select: '',
                month: '',
                remarks: ''
            },
            fileName:'',
            currentpage:1,//当前页数
            pageBean:[//分页参数列表
            ],
            dataTypes: [ //全部数据类型
            ],
            allowDataTypes: [ //启动的数据类型
            ],
            downloadDataTypes: [ //可下载模板且启动的数据类型
            ],
            dataList: [ //数据列表
            ],
            ruleForm: { //form v-model
                region: '',
                selectMonth: '',
                files:'',
                remarks:''
            },
            templateRuleForm: { //form v-model
                datatype: '',
                templateName:''
            },
            rules: { //from 验证
                region: [
                    {required: true, message: '请选择数据类型', trigger: 'change'}
                ],
                selectMonth: [
                    {required: true,type: 'date', message: '请选择月份', trigger: 'change'}
                ]
            },
            tempalteRules: { //from 验证
            	datatype: [
                    {required: true, message: '请选择数据类型', trigger: 'change'}
                ]
            }
        };
    },
    created: function () {
    	//初始化全部数据类型
    	this.initDataTypes();
    	//初始化启动的数据类型
    	this.initAllowDateTypes();
    	//可下载模板的数据类型
    	this.initDownloadDataTypes();
    	
    	//查询第一页数据上报信息
    	this.findDataReportByPage(1);
    },
    methods: {
    	changeFile:function(){
    		this.fileName=$('#file')[0].files[0].name;
    	},
    	/**下一页 上一页按钮 */
      	handleCurrentChange:function(val){
      		this.currentpage=val;
    		this.findDataReportByPage(val,'handleCurrentChange');
      	},
    	closeDialog: function (formName) {
    		this.$refs[formName].resetFields()
    		var file = document.getElementById('file')
    	    file.value=''
    	    file.outerHTML = file.outerHTML
    	},
    	closeTemplateDialog: function (templateRuleForm) {
    		this.$refs[templateRuleForm].resetFields()
    	},
    	selectDataType: function(templateRuleForm){
    		_this = this;
    		console.info(_this.templateRuleForm.datatype);
    		if(_this.templateRuleForm.datatype!='') {
    			$.ajax({
                    url: '/datatype/findDataTypesById',
                    data:{"id":_this.templateRuleForm.datatype},
                    type: 'post',
                    dataType: 'json',
                    success: function (res) {
                    	var name = res.data.templetpath;
                    	_this.templateRuleForm.templateName = name.substring(name.lastIndexOf("/")+1,name.length);
                    },
                    error: function (err) {
                        if(err.responseJSON.message=="session clean"){
                            errorMessage(_this.$message, '登陆已失效重新登陆!');
                            toLogin();
                            return;
                        }
                        errorMessage(_this.$message,err);
                    }
                });
    		}
    	},
    	initAllowDateTypes: function(){
    		_this = this;
        	$.ajax({
                url: '/datatype/findDataTypesByState',
                data:{"state":1},
                type: 'post',
                dataType: 'json',
                success: function (res) {
                	_this.allowDataTypes = res.data;
                },
                error: function (err) {
                    if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
                    errorMessage(_this.$message,err);
                }
            });
    	},
    	initDataTypes: function() {
    		_this = this;
        	$.ajax({
                url: '/datatype/findAllDataTypes',
                type: 'post',
                dataType: 'json',
                success: function (res) {
                	_this.dataTypes = res.data;
                },
                error: function (err) {
                    if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
                    errorMessage(_this.$message,err);
                }
            });
    	},
    	initDownloadDataTypes: function(){
    		_this = this;
        	$.ajax({
                url: '/datatype/findDownloadDataTypes',
                type: 'post',
                dataType: 'json',
                success: function (res) {
                	_this.downloadDataTypes = res.data;
                },
                error: function (err) {
                    if(err.responseJSON.message=="session clean"){
                        errorMessage(_this.$message, '登陆已失效重新登陆!');
                        toLogin();
                        return;
                    }
                    errorMessage(_this.$message,err);
                }
            });
    	},
    	resetSearchForm: function (formName) {
    		this.$refs[formName].resetFields()
    		this.currentpage = 1;
    		this.findDataReportByPage(1);
    	},
    	downloadTemplet: function (templateRuleForm) {
    		_this = this;
    		this.$refs[templateRuleForm].validate(function (valid) {
                 if (valid) {
                	 var id = _this.templateRuleForm.datatype;
                	 if(id!=''||id!=undefined||id!=null) {
                		window.location.href='/datatype/downloadTemplate?id='+id
                	 }
                 } else {
                	 return false;
                 }
    		})
    	},
    	downloadFile: function (id) {
       	 if(id!=''&&id!=undefined&&id!=null) {
       		window.location.href='/datareport/downloadFile?id='+id
       	 }else{
       		 errorMessage(_this.$message, '请选择下载项!');
       	 }
    	},
    	findDataReportByPage: function(page,handleEvent){
    		_this = this;
    		if(handleEvent!='handleCurrentChange') this.currentpage = 1;//初始化到第一页
    		var _select = [];
			if(_this.searchForm.select!="") _select = _this.searchForm.select;
    		var data = {"typeIds":_select,"create_time":formatDateYYYYMM(_this.searchForm.month),"currentPage":page};
        	console.info(JSON.stringify(data));
    		$.ajax({
	             url: '/datareport/findDataReportByPage',
	             type: 'post',
	             data:{"data":JSON.stringify(data)},
	             dataType: 'json',
	             success: function (res) {
	            	_this.exhibition(res);
	             },
	             error: function (err) {
	                 if(err.responseJSON.message=="session clean"){
	                     errorMessage(_this.$message, '登陆已失效重新登陆!');
	                     toLogin();
	                     return;
	                 }
                     if(err.status == 403){
                         errorMessage(_this.$message, "没有权限，请先申请该权限！");
                         return;
                     }
	                 errorMessage(_this.$message,err);
	             }
	         })
    	},
    	/**
         * 
         * 处理页面数据展示
         * */
        exhibition: function(res){
        	var _this = this;
        	var datareport_arr=[];
        	res.data.beanList.forEach(function (obj) {
                var datatype = {};
                datatype['id']=obj.id;
                datatype['code_NAME']=obj.codename;
                datatype['tosname']=obj.tosname;
                datatype['tucname']=obj.tucname;
                datatype['create_TIME']= obj.createtime;
                datareport_arr.push(datatype);
            })
            _this.dataList = datareport_arr;
            var page = {}
            page['totalNumber'] = res.data.totalNumber
            page['totalPage'] = res.data.totalPage
            _this.pageBean = page
        },
        showUploadData: function () {
        	this.dialogVisible = true;
        	
        },
        showDownloadData: function () {
        	this.templatDialogVisible = true;
        },
        saveForm: function (ruleForm) {
            var _this = this;
            this.$refs[ruleForm].validate(function (valid) {
                if (valid) {
                	if ($('#file')[0].files[0] == undefined || $('#file')[0].files[0] == null) {
                		errorMessage(_this.$message, "请选择上传文件");
					} else {
						if($('#file')[0].files[0].size/1024/1024 > 10){
							errorMessage(_this.$message, "允许最大上传文件大小为10M");
							return;
						}
						console.info('添加数据上报');
	                	$.ajax({
	                        url: '/datareport/addDataReport',
	                        data:{"data":JSON.stringify(_this.ruleForm)},
	                        type: 'post',
	                        dataType: 'json',
	                        async:false,
	                        success: function (res) {
	                          if (res.data.status=='1') {
	                      		  errorMessage(_this.$message, res.data.msg);
	                      	  } else {
	                      		  
	                      		 //上传文件
								var formData = new FormData();
	    						formData.append('file', $('#file')[0].files[0]);
	    						formData.append('id',res.data.id);
	    						formData.append('type_id',_this.ruleForm.region);
	    						$.ajax({
	    						    url: '/datareport/uploadFile',
	    						    type: 'POST',
	    						    cache: false,
	    						    data: formData,
	    						    processData: false,
	    						    contentType: false,
	    						    async:false
	    						}).done(function(res) {
                                    if (res.data.status=='0') {
                                        successMessage(_this.$message, res.data.msg);
                                        //隐藏弹框
        	    						_this.dialogVisible = false;
        	    						_this.findDataReportByPage(1);
                                        return;
                                    }
                                    if (res.data.status=='1') {
                                        errorMessage(_this.$message, res.data.msg);
                                        return;
                                    }
	    						}).fail(function(res) {
	    							errorMessage(_this.$message, res);
	    						});
	    						
	    						
	    						
	                      	  }
	                        },
	                        error: function (err) {
	                            if(err.responseJSON.message=="session clean"){
	                                errorMessage(_this.$message, '登陆已失效重新登陆!');
	                                toLogin();
	                                return;
	                            }
                                if(err.status == 403){
                                    errorMessage(_this.$message, "没有权限，请先申请该权限！");
                                    return;
                                }
	                            errorMessage(_this.$message,err);
	                        }
	                    });
					}
                } else {
                    return false
                }
            })
        }
    }
})

